package com.jspgou.cms.action.front;

import static com.jspgou.cms.Constants.MEMBER_SYS;
import static com.jspgou.core.web.Constants.BACK_URL;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jspgou.cms.entity.Address;
import com.jspgou.cms.entity.Cart;
import com.jspgou.cms.entity.CartItem;
import com.jspgou.cms.entity.Order;
import com.jspgou.cms.entity.OrderItem;
import com.jspgou.cms.entity.Payment;
import com.jspgou.cms.entity.PopularityGroup;
import com.jspgou.cms.entity.PopularityItem;
import com.jspgou.cms.entity.Product;
import com.jspgou.cms.entity.ProductFashion;
import com.jspgou.cms.entity.Shipping;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.entity.ShopMemberAddress;
import com.jspgou.cms.entity.Store;
import com.jspgou.cms.manager.AddressMng;
import com.jspgou.cms.manager.CartItemMng;
import com.jspgou.cms.manager.CartMng;
import com.jspgou.cms.manager.MemberCouponMng;
import com.jspgou.cms.manager.OrderMng;
import com.jspgou.cms.manager.PaymentMng;
import com.jspgou.cms.manager.PopularityGroupMng;
import com.jspgou.cms.manager.PopularityItemMng;
import com.jspgou.cms.manager.ProductFashionMng;
import com.jspgou.cms.manager.ProductMng;
import com.jspgou.cms.manager.ShippingMng;
import com.jspgou.cms.manager.ShopMemberAddressMng;
import com.jspgou.cms.manager.StoreMng;
import com.jspgou.cms.service.ShoppingSvc;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.common.web.ResponseUtils;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;

@Controller
public class CartAct {
	private static final Logger log = LoggerFactory.getLogger(CartAct.class);
	// 购物车
	private static final String SHOPPING_CART = "tpl.shoppingCart";
	private static final String CHECKOUT_SHIPPING = "tpl.checkoutShipping";
	private static final String SEASONPASS = "tpl.seasonpass";
	private static final String BUY_NOW = "tpl.buyNow";


	// 购物车
	@RequestMapping(value = "/cart/shopping_cart.jspx")
	public String shoppingCart(String backUrl, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		ShopMember member = CmsUtils.getMember(request);
		if (member == null) {
			return "redirect:../login.jspx";
		}
		Website web = SiteUtils.getWeb(request);
		Cart cart = shoppingSvc.getCart(member, request, response);
		List<PopularityItem> popularityItems = null;
		if (cart != null) {
			popularityItems = popularityItemMng.getlist(cart.getId(), null);
		}
		model.addAttribute("cart", cart);
		if (!StringUtils.isBlank(backUrl)) {
			model.addAttribute(BACK_URL, backUrl);       
		}
		model.addAttribute("popularityItems", popularityItems);
		ShopFrontHelper.setCommonData(request, model, web);
		return web.getTplSys(MEMBER_SYS,
				MessageResolver.getMessage(request, SHOPPING_CART), request);
	}

	// 加入购物车,优惠套装
	@RequestMapping(value = "/cart/add_popularity.jspx")
	public String addToPopularity(Long popularityId, Long pId, Long[] fashId,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		ShopMember member = CmsUtils.getMember(request);
		if (member == null) {
			return "redirect:../login.jspx";
		}
		Website web = SiteUtils.getWeb(request);
		if (getinventory(popularityId)) {
			Cart cart = null;
			// 得到所有商品
			Set<Product> products = popularityGroupMng.findById(popularityId)
					.getProducts();
			for (Product product : products) {

				// 这里加入判断，如果有商品被禁售，则返回商品页。
				if (product.getProhibit()) {
					cart = shoppingSvc.collectAddToCart(product,
							getFashId(product, fashId), popularityId, 1, true,
							member, web, request, response);
				} else {
					return "redirect:" + productMng.findById(pId).getUrl();// 如果有禁售商品，则返回该商品页面
				}
			}
			for (Product product : popularityGroupMng.findById(popularityId)
					.getProducts()) {
				if (product.getOnSale()) {
					cart = shoppingSvc.collectAddToCart(product,
							getFashId(product, fashId), popularityId, 1, true,
							member, web, request, response);
				} else {
					return "redirect:" + productMng.findById(pId).getUrl();
				}
			}
			popularityItemMng.save(cart, popularityId);
		}

		return "redirect:../cart/shopping_cart.jspx";
	}

	public Long getFashId(Product product, Long[] fashIds) {
		Long fashId = null;
		if (fashIds != null) {
			for (Long id : fashIds) {
				if (productFashionMng.findById(id).getProductId()
						.equals(product.getId())) {
					fashId = id;
				}
			}
		}
		return fashId;
	}

	public boolean getinventory(Long popularityId) {
		for (Product product : popularityGroupMng.findById(popularityId)
				.getProducts()) {
			if (1 > product.getStockCount()) {
				return false;
			}
		}
		return true;
	}

	// 加入购物车，组合商品
	@RequestMapping(value = "/cart/add_combinations.jspx")
	public void addToCombinations(Long productId, String ids,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) throws JSONException {
		Website web = SiteUtils.getWeb(request);
		// 0:未登录;1:正常,商品总数;2:数量过大,最大数量;
		ShopMember member = CmsUtils.getMember(request);
		JSONObject json = new JSONObject();
		Long fashId = null;
		Product product = productMng.findById(productId);
		Long[] productIds = getProductIds(ids);
		Cart cart;

		if (product.getProductFashion() != null) {
			fashId = product.getProductFashion().getId();
		}
		int count = 0;// 计算是否有商品已经禁售
		if (member == null) {
			json.put("status", 0);
		} else if (1 > product.getStockCount() && getinventorys(productIds)) {
			json.put("status", 2);
			json.put("error", "库存不足");
		} else if (product.getProhibit() && product.getOnSale()) {
			cart = shoppingSvc.collectAddToCart(product, fashId, null, 1, true,
					member, web, request, response);
			if (productIds != null) {
				for (long pId : productIds) {
					// 对组合中每个商品进行判断，如果有禁售商品，返回页面，页面刷新
					Product product2 = productMng.findById(pId);
					if (product2.getProhibit() && product2.getOnSale()) {
						Long fId = null;
						if (productMng.findById(pId).getProductFashion() != null) {
							fId = productMng.findById(pId).getProductFashion()
									.getId();
						}
						cart = shoppingSvc.collectAddToCart(
								productMng.findById(pId), fId, null, 1, true,
								member, web, request, response);
					} else {
						count++;
					}
				}
			}
			json.put("status", 1);
			if (count > 0) {
				json.put("status", 3);
				json.put("error", "组合中有商品被禁售或下架，请重新确认");
			}
		} else if (product.getOnSale()) {
			json.put("status", 4);
			json.put("error", "主产品被禁售");
		} else if (product.getProhibit()) {
			json.put("status", 5);
			json.put("error", "主产品已下架");
		}
		ResponseUtils.renderJson(response, json.toString());

	}

	public boolean getinventorys(Long[] productIds) {
		if (productIds != null) {
			for (long pId : productIds) {
				Product product = productMng.findById(pId);
				if (product != null) {
					if (1 > product.getStockCount()) {
						return false;
					}
				}
			}
		}
		return true;
	}

	public Long[] getProductIds(String rightlist) {
		String[] arr;
		arr = rightlist.split(",");
		Long productIds[] = new Long[arr.length];
		for (int i = 0, j = arr.length; i < j; i++) {
			if (!StringUtils.isBlank(arr[i])) {
				productIds[i] = Long.parseLong(arr[i]);
			}
		}
		if (arr.length == 1) {
			if (StringUtils.isBlank(arr[0])) {
				productIds = null;
			}
		}
		return productIds;
	}

	// 加入购物车
	@RequestMapping(value = "/cart/add_orderItem.jspx")
	public void addToCart(Long productId, Integer productAmount, Long fashId,
			Boolean isAdd, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws JSONException {
		Website web = SiteUtils.getWeb(request);
		// 0:未登录;1:正常,商品总数;2:数量过大,最大数量;
		ShopMember member = CmsUtils.getMember(request);
		JSONObject json = new JSONObject();
		if (member == null) {
			json.put("status", 0);
		} else {
			Product product = productMng.findById(productId);
			// 测试商品已禁售或下架，直接跳转到商品页面
			if (product.getProhibit() && product.getOnSale()) {			
				if (fashId == null) {
					if (productAmount > product.getStockCount()) {
						
						json.put("status", 2);
						json.put("error", "库存不足");
					} else {
						Cart cart = shoppingSvc.collectAddToCart(product,
								fashId, null, productAmount, true, member, web,
								request, response);
						json.put("status", 1);
						json.put("count", cart.getTotalItems());
					}
				} else {
					ProductFashion productFashion = productFashionMng.findById(fashId);
					
					if (productAmount > productFashion.getStockCount()) {
						json.put("status", 2);
						json.put("error", "库存不足");
						
					} else {
						Cart cart = shoppingSvc.collectAddToCart(product,
								fashId, null, productAmount, true, member, web,
								request, response);
						
						json.put("status", 1);
						json.put("count", cart.getTotalItems());
					}
				}
				// 如果商品已经禁售，设置status状态。使前台无法进入购物车，页面刷新。显示商品已经禁售
			} else if (!product.getProhibit() || !product.getOnSale()) {
				json.put("status", 3);
				json.put("error", "非常抱歉，商品刚刚被禁售或下架");
			}
		}
		log.info("add to cart productId={}, count={}", productId, productAmount);

		ResponseUtils.renderJson(response, json.toString());
	}

	// 购买优惠套装第一步调用的后台方法
	@RequestMapping(value = "/cart/season_pass.jspx")
	public String seasonPass(Long popularityId, Long pId,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
		ShopMember member = CmsUtils.getMember(request);
		if (member == null) {
			return "redirect:../login.jspx";
		}

		Website web = SiteUtils.getWeb(request);
		if (popularityId == null) {
			return ShopFrontHelper.showMessage(request, model, "库存不足！");
		}
		if (getinventory(popularityId)) {
			PopularityGroup popularityGroup = popularityGroupMng
					.findById(popularityId);
			Set<Product> products = popularityGroup.getProducts();
			int saleCount = 0;
			// 判断是否有禁售或者下架商品，只要有禁售或下架是商品，就返回页面
			for (Product product : products) {
				if (product.getProhibit() && product.getOnSale()) {
					model.addAttribute("popularityGroup", popularityGroup);
					ShopFrontHelper.setCommonData(request, model, web);
				} else {
					// 只要包含一个下架或禁售商品，就返回页面g
					saleCount++;
				}
			}
			if (saleCount > 0) {
				return "redirect:" + productMng.findById(pId).getUrl();
			}
			model.addAttribute("popularityGroup", popularityGroup);
			ShopFrontHelper.setCommonData(request, model, web);
			model.addAttribute("fromProduct", productMng.findById(pId));
			return web.getTplSys(MEMBER_SYS,
					MessageResolver.getMessage(request, SEASONPASS), request);
		} else {
			return ShopFrontHelper.showMessage(request, model, "库存不足！");
		}
	}

	// 订单再次加入购物车
	@RequestMapping(value = "/cart/add_orderItem1.jspx")
	public String orderAddToCart(Long orderId, Boolean isAdd,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) throws JSONException {
		Website web = SiteUtils.getWeb(request);
		// 0:未登录;1:正常,商品总数;2:数量过大,最大数量;
		ShopMember member = CmsUtils.getMember(request);
		if (member == null) {
			return "redirect:../login.jspx";
		}
		Order order = orderMng.findById(orderId);
		Product product = null;
		Integer productAmount = 0;
		Long fashId = null;
		Cart cart = null;
		for (OrderItem item : order.getItems()) {
			product = item.getProduct();
			productAmount = item.getCount();
			if (item.getProductFash() != null) {
				fashId = item.getProductFash().getId();
			}
			cart = shoppingSvc.collectAddToCart(product, fashId, null,
					productAmount, true, member, web, request, response);
		}
		return "redirect:shopping_cart.jspx";
	}

	// 更新购物车项
	@RequestMapping(value = "/cart/ajaxUpdateCartItem.jspx")
	public void ajaxUpdateCartItem(Long cartItemId, Integer count,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) throws JSONException {
		ShopMember member = CmsUtils.getMember(request);
		JSONObject json = new JSONObject();
		if (member == null) {
			json.put("status", 0);
		}
		CartItem cartItem = cartItemMng.findById(cartItemId);
		cartItem.setCount(count);
		cartItem.setScore(cartItem.getProduct().getScore() * count);
		cartItemMng.updateByUpdater(cartItem);
		log.info("update to cartItem cartItemId={}", cartItemId);
		json.put("status", 1);
		ResponseUtils.renderJson(response, json.toString());
	}

	// 删除购物车项
	@RequestMapping(value = "/cart/ajaxDeleteCartItem.jspx")
	public void ajaxDeleteCartItem(Long cartItemId, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws JSONException {
		ShopMember member = CmsUtils.getMember(request);
		JSONObject json = new JSONObject();
		if (member == null) {
			json.put("status", 0);
		}
		CartItem cartItem = cartItemMng.findById(cartItemId);
		PopularityGroup popularityGroup = cartItem.getPopularityGroup();
		Cart cart = cartItem.getCart();
		cart.getItems().remove(cartItem);
		cart.calTotalItem();
		cartMng.update(cart);
		cartItemMng.deleteById(cartItemId);
		if (cart != null && popularityGroup != null) {
			List<CartItem> list = cartItemMng.getlist(cart.getId(),
					popularityGroup.getId());
			for (CartItem item : list) {
				item.setPopularityGroup(null);
				cartItemMng.updateByUpdater(item);
			}
			update(cart, popularityGroup);
		}
		log.info("delete to cartItem cartItemId={}", cartItemId);
		json.put("status", 1);
		ResponseUtils.renderJson(response, json.toString());
	}

	// 检查商品库存
	@RequestMapping(value = "/cart/checkStockCount.jspx")
	public void checkStockCount(Long productId, Long productFashionId,
			Integer count, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws JSONException {
		ShopMember member = CmsUtils.getMember(request);
		JSONObject json = new JSONObject();
		if (member == null) {
			json.put("status", 0);
		} else {
			Product product = productMng.findById(productId);
			if (productFashionId == null) {
				if (count > product.getStockCount()) {
					json.put("status", 2);
					json.put("error", product.getName() + "该商品库存不足。");
				} else {
					json.put("status", 1);
				}
			} else {
				ProductFashion productFashion = productFashionMng
						.findById(productFashionId);
				if (count > productFashion.getStockCount()) {
					json.put("status", 2);
				} else {
					json.put("status", 1);
				}
			}
		}
		ResponseUtils.renderJson(response, json.toString());
	}

	// 计算运费
		@RequestMapping(value = "/cart/ajaxtotalDelivery.jspx")
		public void ajaxtotalDelivery(Long deliveryMethod,Long storeId,String itemStr,
				HttpServletRequest request, HttpServletResponse response,ModelMap model) throws JSONException {
			ShopMember member=CmsUtils.getMember(request);
			JSONObject json=new JSONObject();
			if (member == null) {
				json.put("status", 0);
			}
			Shipping shipping=shippingMng.findById(deliveryMethod);
			//计算运费
			Double weight = getcartWeight(storeId,itemStr);
			Double freight=shipping.calPrice(weight);
			json.put("status", 1);
			json.put("freight", freight);
			ResponseUtils.renderJson(response, json.toString());
		}
		
	public 	Double getcartWeight(Long storeId,String itemStr){
		Double cartWeight = 0.0;
		List<CartItem> clist = new ArrayList<CartItem>();
		String[] str = itemStr.split(";");
		for(String s:str){
			if(!StringUtils.isBlank(s)){
				if(storeId!=null){
					if(cartItemMng.findById(Long.parseLong(s)).getStore()!=null){
						if(cartItemMng.findById(Long.parseLong(s)).getStore().getId()==storeId){
							clist.add(cartItemMng.findById(Long.parseLong(s)));
						}
					}
				}else{
					if(cartItemMng.findById(Long.parseLong(s)).getStore()==null){
						clist.add(cartItemMng.findById(Long.parseLong(s)));
					}
				}
			}
		}
		for(CartItem item:clist){
			cartWeight = (double) (item.getProduct().getWeight()*item.getCount());
		}
		return cartWeight;
	}
	
	
	// 立即购买
	@RequestMapping(value = "/buyNow.jspx")
	public String buyNow(Long productId,Integer count,
			HttpServletRequest request, HttpServletResponse response,ModelMap model) throws JSONException {
			ShopMember member=CmsUtils.getMember(request);
			if (member == null) {
				return "redirect:../login.jspx";
			}
			Website web = SiteUtils.getWeb(request);
			Product entity = productMng.findById(productId);
			Long storeId = null;
			if(entity.getStore()!=null){
				storeId = entity.getStore().getId();
			}
			Store store=entity.getStore();
			model.addAttribute("store", store);
			Double popularityPrice = 0.0;
			List<Store> slist = new ArrayList<Store>();
			slist.add(entity.getStore());
			
				
			Double price = 0.0;
			if (entity.getProductFashion() != null) {
				price = entity.getProductFashion().getSalePrice() * count;
			} else {
				price = entity.getSalePrice() * count;
			}
			model.addAttribute("memberCouponlist", memberCouponMng.getList(member.getId(), new BigDecimal(price)));
			StringBuffer str = new StringBuffer();	
			String itemStr = str.toString();
			model.addAttribute("itemStr", itemStr);
			model.addAttribute("slist", slist);

			// 会员地址
			List<ShopMemberAddress> smalist = shopMemberAddressMng.getList(member.getId());
			model.addAttribute("smalist", smalist);
			// 所处省份
			List<Address> plist = addressMng.getListById(null);
			model.addAttribute("plist", plist);
			// 付款方式
			List<Payment> paylist = paymentMng.getList((long) 1, true);
			model.addAttribute("paylist", paylist);
			// 配送方式
			model.addAttribute("product", entity);
			model.addAttribute("count", count);
			List<Shipping> splist = shippingMng.getList(web.getId(), storeId, null,
					true);

			model.addAttribute("splist", splist);
			List<CartItem> clist = cartItemMng.findByStore(storeId, member.getId());
			model.addAttribute("clist", clist);
			model.addAttribute("popularityPrice", popularityPrice);
			//model.addAttribute("isSell", isSell(items));
			ShopFrontHelper.setCommonData(request, model, web);
			// 测试确认结账后请求的方法
			return web.getTplSys(MEMBER_SYS,MessageResolver.getMessage(request, BUY_NOW), request);
	}
	
	

	// 选择收货地址、付款方式、配送方式
	@RequestMapping(value = "/cart/checkout_shipping.jspx")
	public String shippingInput( Long storeId, Long[] cart2Checkbox, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member = CmsUtils.getMember(request);
		if (member == null) {
			return "redirect:../login.jspx";
		}
		Cart cart = shoppingSvc.getCart(member.getId());
		if (cart == null) {
			return "redirect:shopping_cart.jspx";
		}
		
		List<PopularityItem> popularityItems = null;
		Double popularityPrice = 0.0;
		Set<Long> set = new HashSet<Long>();
		List<Store> slist = new ArrayList<Store>();
		if (cart != null) {
			int popularityCount1 = 0;
			popularityItems = popularityItemMng.getlist(cart.getId(), null);
			for (PopularityItem popularityItem : popularityItems) {
				popularityPrice += popularityItem.getPopularityGroup()
						.getPrivilege() * popularityItem.getCount();
				// 首先循环遍历每个优惠套装中的商品，查看是否有被下架或禁售商品。
				PopularityGroup popularityGroup = popularityItem
						.getPopularityGroup();
				if (popularityGroup.getOnSale() != null
						&& !popularityGroup.getOnSale()
						|| (popularityGroup.getProhibit() != null && !popularityGroup
								.getProhibit())) {
					popularityItemMng.deleteById(popularityItem.getId());
				}

				if (popularityGroup != null) {
					for (Product product : popularityGroup.getProducts()) {
						if (!product.getProhibit() || !product.getOnSale()) {// 判断优惠套装中是否有商品呗禁售或者下架，如果有,则标志套装禁售或者下架，并且返回页面
							popularityCount1++;
							if (!product.getProhibit()) {
								popularityGroup.setProhibit(false);
							}
							if (!product.getOnSale()) {
								popularityGroup.setOnSale(false);
							}
							popularityGroupMng.update(popularityGroup);
						}
					}
				}

			}
			if (popularityCount1 > 0) {
				return "redirect:shopping_cart.jspx";
			}

			Set<CartItem> items = getItems(cart2Checkbox, cart);
			Double price = getPrice(items);
			model.addAttribute("memberCouponlist", memberCouponMng.getList(member.getId(), new BigDecimal(price)));
			if (cart != null) {
				Set<CartItem> item = cart.getItems();
				List<CartItem> lists = new ArrayList<CartItem>();
				for (CartItem item1 : item) {
					if (item1.getOnSale() != null
							&& !item1.getOnSale()
							|| (item1.getProhibit() != null && !item1
									.getProhibit())) {
						lists.add(item1);
					}
				}
				item.removeAll(lists);

				int onSaleCount = 0;
				for (CartItem item1 : item) {// 循环遍历每个item中的商品,标记有禁售或者下架的cartItem
					Product product = item1.getProduct();
					if (!product.getOnSale() || !product.getProhibit()) {
						onSaleCount++;
						if (!product.getProhibit()) {
							item1.setProhibit(false);
						}
						if (!product.getOnSale()) {
							item1.setOnSale(false);
						}
						cartItemMng.updateByUpdater(item1);// 将标记同步更新到数据库
					}
				}
				if (onSaleCount > 0) {
					return "redirect:shopping_cart.jspx";
				}
			}

			for (CartItem ci : items) {
				if (ci.getStore() != null) {
					set.add(ci.getStore().getId());
				}
			}
			for (Long id1 : set) {
				slist.add(storeMng.findById(id1));
			}
		}
		
		StringBuffer str = new StringBuffer();
		/*for(int i=0;i<cart2Checkbox.length;i++){
			if(i==0){
				str.append( cart2Checkbox[i]);
			}else{
				str.append(";"+cart2Checkbox[i]);
			}
		 }*/	
		String itemStr = str.toString();
		model.addAttribute("itemStr", itemStr);
		model.addAttribute("slist", slist);

		model.addAttribute("cart", cart);
		// 会员地址
		List<ShopMemberAddress> smalist = shopMemberAddressMng.getList(member
				.getId());
		model.addAttribute("smalist", smalist);
		// 所处省份
		List<Address> plist = addressMng.getListById(null);
		model.addAttribute("plist", plist);
		// 付款方式
		List<Payment> paylist = paymentMng.getList((long) 1, true);
		model.addAttribute("paylist", paylist);
		// 配送方式
		model.addAttribute("items", getItems(cart2Checkbox, cart));
		List<Shipping> splist = shippingMng.getList(web.getId(), storeId, null,
				true);

		model.addAttribute("splist", splist);
		List<CartItem> clist = cartItemMng.findByStore(storeId, member.getId());
		model.addAttribute("clist", clist);
		model.addAttribute("popularityPrice", popularityPrice);
		model.addAttribute("isSell", isSell(getItems(cart2Checkbox, cart)));
		ShopFrontHelper.setCommonData(request, model, web);
		// 测试确认结账后请求的方法
		return web.getTplSys(MEMBER_SYS,MessageResolver.getMessage(request, CHECKOUT_SHIPPING), request);
	}

	public Boolean isSell(Set<CartItem> items) {
		for (CartItem item : items) {
			if (item != null) {
				if (item.getProduct().getStore() == null) {
					return true;
				}
			}
		}
		return false;
	}

	public java.util.Set<com.jspgou.cms.entity.CartItem> getItems(
			Long[] cart2Checkbox, Cart cart) {
		Set<CartItem> items = new HashSet<CartItem>();
		if (cart2Checkbox != null) {
			for (Long id : cart2Checkbox) {
				items.add(cartItemMng.findById(id));
			}
		} else {
			items = cart.getItems();
		}
		return items;
	}

	public void update(Cart cart, PopularityGroup popularityGroup) {
		if (popularityGroup != null) {
			PopularityItem popularityItem = popularityItemMng.findById(
					cart.getId(), popularityGroup.getId());
			if (popularityItem != null) {
				popularityItemMng.deleteById(popularityItem.getId());
			}

		}
	}

	@RequestMapping(value = "/clearCart.jspx")
	public void clearCart(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) throws JSONException {
		ShopMember member = CmsUtils.getMember(request);
		JSONObject json = new JSONObject();
		if (member == null) {
			json.put("status", 0);
		}
		shoppingSvc.clearCart(member);
		json.put("status", 1);
		json.put("success", "成功");
		ResponseUtils.renderJson(response, json.toString());
	}

	public Double getPrice(Set<CartItem> items) {
		Double price = 0.00;
		for (CartItem item : items) {
			if (item.getProductFash() != null) {
				price += item.getProductFash().getSalePrice() * item.getCount();
			} else {
				price += item.getProduct().getSalePrice() * item.getCount();
			}
		}
		return price;
	}
	
	public String getAddress(ShopMemberAddress address){
		String str = "";
		if(address.getProvince()!=null){
			str = str + address.getProvince()+"-";
		}
		if(address.getCity()!=null){
			str = str + address.getCity()+"-";
		}
		if(address.getCountry()!=null){
			str = str + address.getCountry()+"-";
		}
		str = str+address.getDetailaddress();
		return str;
	}

	@Autowired
	private OrderMng orderMng;
	@Autowired
	private ShoppingSvc shoppingSvc;
	@Autowired
	private ProductMng productMng;
	@Autowired
	private ProductFashionMng productFashionMng;
	@Autowired
	private CartItemMng cartItemMng;
	@Autowired
	private CartMng cartMng;
	@Autowired
	private StoreMng storeMng;
	@Autowired
	private AddressMng addressMng;
	@Autowired
	private PaymentMng paymentMng;
	@Autowired
	private ShippingMng shippingMng;
	@Autowired
	private ShopMemberAddressMng shopMemberAddressMng;
	@Autowired
	private PopularityGroupMng popularityGroupMng;
	@Autowired
	private PopularityItemMng popularityItemMng;
	
	@Autowired
	private MemberCouponMng memberCouponMng;

}
